/*
 * @Author: caijw 
 * @Date: 2018-11-04 23:37:13 
 * @Last Modified by: caijw
 * @Last Modified time: 2018-12-02 23:41:40
 */
import * as types from '../action-types';
export default function reducer(state={
  user: [],
  sign : {},
  cardList : {
    list : []
  }, //卡片列表
  collectionList : [],//收藏列表
  viewList : [], //浏览记录
  message : {
    messageList : [],
    page : 1
  },
  view : {//浏览记录
    viewList : [],
    page : 1
  },
  collection : {//收藏记录
    collectionList : [],
    page : 1
  },
  order : {
    orderList : [],
    page : 1,
    type : 0
  },
  money : {
    ret : 1,
    data : {
      data : {
        account : '',
        
      },
      money : 0
    }
  },
  addressRet : 1,
  addressMsg : '',
  addressList : [],
  addressDetail : {},
  getMoneyRet : 1,
  userRet : 1,
  nameRet : 1,
  fetching: false,
  fetched: false,
  error: null,
}, action) {
  //判断类型做出对应state的改变
  switch (action.type){
    case types.FETCH_USER: {
      return {...state, fetching: true,fetched: false}
    }
    case types.FETCH_USER_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true,
          user: action.payload.data,
          userRet : action.payload.ret
        }
      }
    }
    case types.POST_SIGN: {
      return {...state, fetching: true,fetched: false}
    }
    case types.POST_SIGN_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true,
          sign: action.payload
        }
      }
    }
    //地址新增
    case types.POST_ADDRESS: {
      return {...state, fetching: true,fetched: false, addressRet: -1}
    }
    case types.POST_ADDRESS_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true,
          addressRet : action.payload.ret,
          addressMsg : action.payload.msg
        }
      }
    }
    //修改地址
    case types.PATH_ADDRESS: {
      return {...state, fetching: true,fetched: false, addressRet: -1}
    }
    case types.PATH_ADDRESS_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true,
          addressRet : action.payload.ret,
          addressMsg : action.payload.msg
        }
      }
    }
    //获取地址明细
    case types.GET_ADDRESS_DETAIL: {
      return {...state, fetching: true,fetched: false, addressRet: 0 }
    }
    case types.GET_ADDRESS_DETAIL_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true,
          addressDetail : action.payload.data
        }
      }
    }
    //地址删除
    case types.DELETE_ADDRESS: {
      return {...state, fetching: true,fetched: false}
    }
    case types.DELETE_ADDRESS_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true
        }
      }
    }
    //地址列表
    case types.GET_ADDRESS: {
      return {...state, fetching: true,fetched: false, addressRet: -1}
    }
    case types.GET_ADDRESS_FULFILLED: {
      if(action.payload.data){
        return {
          ...state,
          fetching: false,
          fetched: true,
          addressList : action.payload.data
        }
      }
    }

    //改变名字
    case types.POST_CHANGE_NAME: {
      return {...state, fetching: true,fetched: false, nameRet : 1}
    }
    case types.POST_CHANGE_NAME_FULFILLED: {
      return {
        ...state,
        fetching: false,
        fetched: true,
        nameRet : action.payload.ret,
        nameMsg : action.payload.msg
      }
    }

    case types.POST_CARD: {
      return {...state, fetching: true, cardRet : -1}
    }
    case types.POST_CARD_FULFILLED: {  
        return {
          ...state,
          fetching: false,
          fetched: true,
          cardRet : action.payload.ret,
          cardRetMsg : action.payload.msg
        }
    }
    //充值列表
    case types.GET_CARD_LIST: {
      return {...state, fetching: true}
    }
    case types.GET_CARD_LIST_FULFILLED: {
      return {
        ...state,
        fetching: false,
        fetched: true,
        cardList : action.payload.data
      }
    }
    //收藏列表
    case types.GET_COLLECTION_LIST: {
      return {...state, fetching: true}
    }
    case types.GET_COLLECTION_LIST_FULFILLED: {
      return {
        ...state,
        fetching: false,
        fetched: true,
        collection: {
          collectionList: [...state.collection.collectionList,...action.payload.data.list],
          page : state.collection.page + 1
        }
      }
    }
    //浏览
    case types.GET_VIEW_LIST: {
      return {...state, fetching: true}
    }
    case types.GET_VIEW_LIST_FULFILLED: {
      return {
        ...state,
        fetching: false,
        fetched: true,
        view: {
          viewList: [...state.view.viewList,...action.payload.data.list],
          page : state.view.page + 1
        }
      }
    }
    //关于
    case types.GET_ABOUT: {
      return {...state, fetching: true}
    }
    case types.GET_ABOUT_FULFILLED: {
      return {
        ...state,
        fetching: false,
        fetched: true,
        content : action.payload.data.content
      }
    }
    //我的余额
    case types.GET_MY_MONEY: {
      return {...state, fetching: true}
    }
    case types.GET_MY_MONEY_FULFILLED: {
      return {
        ...state,
        fetching: false,
        fetched: true,
        money : action.payload
      }
    }
    //绑定卡片
    case types.BIND_CARD: {
      return {...state, fetching: true, bindCardRet: -1}
    }
    case types.BIND_CARD_FULFILLED: {
      return {
        ...state,
        fetching: false,
        fetched: true,
        bindCardRet : action.payload.ret
      }
    }
    //体现
    case types.GET_MONEY: {
      return {...state, fetching: true, getMoneyRet: 2}
    }
    case types.GET_MONEY_FULFILLED: {
      return {
        ...state,
        fetching: false,
        fetched: true,
        getMoneyRet : action.payload.ret,
        getMoneyMsg : action.payload.msg
      }
    }

    //消息
    case types.GET_MESSAGE: {
      return {...state, fetching: true, getMoneyRet: -1}
    }
    case types.GET_MESSAGE_FULFILLED: {
      return {
        ...state,
        fetching: false,
        fetched: true,
        message : {
          messageList: [...state.message.messageList,...action.payload.data.push_message_list],
          page : state.message.page + 1
        }
      }
    }

    //订单
    case types.GET_ORDER: {
      return {...state, fetching: true, getMoneyRet: -1}
    }
    case types.GET_ORDER_FULFILLED: {
      return {
        ...state,
        fetching: false,
        fetched: true,
        order : {
          orderList: [...state.order.orderList,...action.payload.data.list],
          page : state.order.page + 1,
          type : state.order.type
        }
      }
    }
  }
  return state;
}